<?php

class Model_User extends RL_Model
{
	protected function init() {

	}

	public function setRoleByOid($community_oid, $user_oid, $role_oid) {
		$res = $this->acl_ass->setRoleByOid($community_oid, $user_oid, $role_oid);
		if ($res) {
			/*
			$store = $this->ctx->store;
			$user = $store->find('select objects where user.oid = :0', array($user_oid))->next();
			$user->data['role'] = $role_oid;
			$user->save();
			$user->inSense('user');
			return $user;
			*/
			return true;
		}
		return NULL;
	}

	// прописывает юзера зарегистрированным на узле в корень узла
	public function addNewUser($cred, $role_name='registered')
	{
		$u = array();
		$u['login']      = isset($cred['login'])      ? $cred['login']      : NULL;
		$u['password']   = isset($cred['password'])   ? $cred['password']   : NULL;
		$u['realm']      = isset($cred['realm'])      ? $cred['realm']      : '';

		$u['fullname']   = isset($cred['fullname'])   ? $cred['fullname']   : $u['login'];
		$u['nick']       = isset($cred['nick'])       ? $cred['nick']       : '';
		$u['bdate']      = isset($cred['bdate'])      ? $cred['bdate']      : '';

		$u['openid_url'] = isset($cred['openid_url']) ? $cred['openid_url'] : '';
		$u['email']      = isset($cred['email'])      ? $cred['email']      : '';

		// FIXME : 2008-09-18 : lucky
		//
		$u['avatar']     = '';
		$u['sent_invitations']     = 0;
		$u['status']     = '';

		$user = $this->store->create('fuser', $u);
		$res = $user->save();
		$res = $user->inSense("user");
		$res = $user->save();

		$this->acl_ass->addNewUser($user->id, $u['fullname'], $role_name);

		return $user;
	}

	public function updateUserData($oid, $data) {
		$user = $this->store->find('select objects where oid = :oid', array('oid' => $oid))->next();
		if ($user) {
			// HACK : 2007-11-21 : lucky
			// не допускаем NULL'ы иначе может обламаться inSense
			foreach ($data as $k=>$v) {
				if (is_null($v)) unset($data[$k]);
			}
			$user->data = array_merge($user->data, $data);
			$user->save();
			$user->store->delProperty($user->id, 'user');
			$res = $user->inSense("user");
		}
		return $user->data;
	} // end of function updateUserData

	public function updateUser( $object ) {
		$res1 = $object->save();
		$res2 = $object->updateSense( 'user' );

		if (($res1) && ($res2)) return true;
			else return false;
	}

	public function getUser( $user_oid ) {
		// поиск автора
		$qau['user']['oid']['=='] = $user_oid;
		$rs = $this->ctx->store->find( $qau );
		$user = $rs->next();

		return $user;
	}

	public function findUserByOpenID ( $openid_url )
	{
		$user_obj = NULL;
		// поиск автора
		if ($user_obj = $this->ctx->store->find('select where user.openid_url = :0', array($openid_url))->next()) {
			$user_obj->data['oid'] = $user->id;
			$user_obj->data['url'] = $this->hrefTo($user_obj);
		}

		return $user_obj;
	}

	public function getPersons ($user_obj)
	{
		$persons = array();
		if (isset($user_obj->data['openid_url'])) {
			$openid_url = $user_obj->data['openid_url'];
			$persons = $this->ctx->mm->getModel('Person', 'm')->findPersonsByOpenID($openid_url);
		}
		return $persons;
	} // end of method getUser

	public function hrefTo ($user_obj, $params=array())
	{
		$rule = 'showfreemember';
		$router = $this->ctx->rh->router;

		$ug = new RL_URLGenerator($router->getRule($rule));
		$ug->setRouter($router);
		$params['user_url'] = $user_obj->id;
		$ug->setParams($params);
		$url = $ug->generateUrl();
		return $url;
	} // end of method hrefTo
}
